//
//  NetServer.m
//  Word Memory
//
//  Created by TianWan on 15/11/2.
//  Copyright (c) 2015年 天玩. All rights reserved.
//

#import "NetServer.h"
#import <SVProgressHUD.h>
#import <SystemConfiguration/SystemConfiguration.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "CustomToastView.h"
@interface NetServer()<NSURLConnectionDelegate>

@end

@implementation NetServer

+ (instancetype)shareInstance
{
    static NetServer *sharedManager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        sharedManager = [[NetServer alloc] init];
    });
    return sharedManager;
    
}

- (void)POST:(NSString *)url parameters:(id)params userAgent:(NSString *)userAgent uuid:(NSString *)uuid resultBlock:(ResultBlock)block errorBlock:(ErrorBlock)errorBlock withDialog:(BOOL)show model:(id)model
{
    if(show) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD show];
        });
    }
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:userAgent forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:uuid forHTTPHeaderField:@"uuid"];
    manager.requestSerializer.timeoutInterval = 15.f;
    
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if(show){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
        }
        
        if(responseObject){
            block(responseObject);
            NSLog(@"%@",responseObject);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if(show){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
        }
        
        if (error) {
            errorBlock(error);
        }

    }];
}

- (void)MapPOST:(NSString *)url parameters:(id)params userAgent:(NSString *)userAgent uuid:(NSString *)uuid resultBlock:(ResultBlock)block errorBlock:(ErrorBlock)errorBlock withDialog:(BOOL)show model:(id)model
{
    if(show) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD show];
        });
    }
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [manager.requestSerializer setValue:userAgent forHTTPHeaderField:@"User-Agent"];
    [manager.requestSerializer setValue:uuid forHTTPHeaderField:@"uuid"];
    manager.requestSerializer.timeoutInterval = 15.f;
    manager.securityPolicy.allowInvalidCertificates = YES;
    
    
    [manager POST:url parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        if(show){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
        }
        
        if (operation.response.statusCode == 200) {
            block(@"success");
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        if(show){
            dispatch_async(dispatch_get_main_queue(), ^{
                [SVProgressHUD dismiss];
            });
        }
        
        if (error) {
            errorBlock(error);
        }
        
    }];
}




@end
